home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / mipscoff.c,v < prev    next >
Encoding:
Text File  |  1992-07-22  |  56.9 KB  |  2,133 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    jhh:1.4; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     92.05.28.11.39.09;  author jhh;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     91.12.19.14.09.44;  author jhh;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     91.07.22.23.15.33;  author jhh;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     91.07.02.17.27.03;  author jhh;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @@
  32.  
  33.  
  34. 1.4
  35. log
  36. @got rid of complaint about unimplemented void type
  37. @
  38. text
  39. @/* Read coff symbol tables and convert to internal format, for GDB.
  40.    Design and support routines derived from dbxread.c, and UMAX COFF
  41.    specific routines written 9/1/87 by David D. Johnson, Brown University.
  42.    Revised 11/27/87 ddj@@cs.brown.edu
  43.    Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.
  44.  
  45. This file is part of GDB.
  46.  
  47. GDB is free software; you can redistribute it and/or modify
  48. it under the terms of the GNU General Public License as published by
  49. the Free Software Foundation; either version 1, or (at your option)
  50. any later version.
  51.  
  52. GDB is distributed in the hope that it will be useful,
  53. but WITHOUT ANY WARRANTY; without even the implied warranty of
  54. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  55. GNU General Public License for more details.
  56.  
  57. You should have received a copy of the GNU General Public License
  58. along with GDB; see the file COPYING.  If not, write to
  59. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  60.  
  61.  
  62. #include <a.out.h>
  63. #include <stdio.h>
  64. #include "symtab.h"
  65. #include <sys/file.h>
  66.  
  67. PDR *proc_desc_table = NULL;
  68. long proc_desc_length = 0;
  69.  
  70. static void add_symbol_to_list ();
  71. static int read_coff_symtab ();
  72. static void patch_opaque_types ();
  73. static struct type *decode_function_type ();
  74. static struct type *decode_type ();
  75. static struct type *decode_base_type ();
  76. static struct type *read_enum_type ();
  77. static struct type *read_struct_type ();
  78. static struct type *read_type ();
  79. static void finish_block ();
  80. static struct blockvector *make_blockvector ();
  81. static struct symbol *process_coff_symbol ();
  82. static char *getfilename ();
  83. static char *getsymname ();
  84.  
  85. extern int fclose ();
  86. extern void close ();
  87. extern void free_all_symtabs ();
  88. extern void free_all_psymtabs ();
  89.  
  90.  
  91. /* Name of source file whose symbol data we are now processing.
  92.    This comes from a symbol named ".file".  */
  93.  
  94. static char *last_source_file;
  95.  
  96. /* Core address of the end of the first object file.  */
  97. static CORE_ADDR first_object_file_end;
  98.  
  99. /* End of the text segment of the executable file,
  100.    as found in the symbol _etext.  */
  101.  
  102. static CORE_ADDR end_of_text_addr;
  103.  
  104. /* The end address of the last seen procedure. */
  105.  
  106. static CORE_ADDR last_end_addr;
  107.  
  108. /* The file, a.out  and text section headers of the symbol file */
  109.  
  110. static FILHDR file_hdr;
  111. static SCNHDR text_hdr;
  112. static AOUTHDR aout_hdr;
  113.  
  114. /* The index in the symbol table of the last coff symbol that was processed.  */
  115.  
  116. static int symnum;
  117.  
  118. /* Vector of types defined so far, indexed by their coff symnum.  */
  119.  
  120. static struct typevector *type_vector;
  121.  
  122. /* Number of elements allocated for type_vector currently.  */
  123.  
  124. static int type_vector_length;
  125.  
  126. /* Chain of typedefs of pointers to empty struct/union types.
  127.    They are chained thru the SYMBOL_VALUE.  */
  128.  
  129. #define HASHSIZE 127
  130. static struct symbol *opaque_type_chain[HASHSIZE];
  131.  
  132. /* Record the symbols defined for each context in a list.
  133.    We don't create a struct block for the context until we
  134.    know how long to make it.  */
  135.  
  136. struct pending
  137. {
  138.   struct pending *next;
  139.   struct symbol *symbol;
  140. };
  141.  
  142. /* Here are the three lists that symbols are put on.  */
  143.  
  144. struct pending *file_symbols;    /* static at top level, and types */
  145.  
  146. struct pending *global_symbols;    /* global functions and variables */
  147.  
  148. struct pending **global_symbols_all, **file_symbols_all;
  149.  
  150. struct pending *local_symbols;    /* everything local to lexical context */
  151.  
  152. /* List of unclosed lexical contexts
  153.    (that will become blocks, eventually).  */
  154.  
  155. struct context_stack
  156. {
  157.   struct context_stack *next;
  158.   struct pending *locals;
  159.   struct pending_block *old_blocks;
  160.   struct symbol *name;
  161.   CORE_ADDR start_addr;
  162. };
  163.  
  164. struct context_stack *context_stack;
  165.  
  166. /* Nonzero if within a function (so symbols should be local,
  167.    if nothing says specifically).  */
  168.  
  169. int within_function;
  170.  
  171. /* List of blocks already made (lexical contexts already closed).
  172.    This is used at the end to make the blockvector.  */
  173.  
  174. struct pending_block
  175. {
  176.   struct pending_block *next;
  177.   struct block *block;
  178. };
  179.  
  180. struct pending_block *pending_blocks;
  181.  
  182. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  183. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  184.  
  185. /* File name symbols were loaded from.  */
  186.  
  187. static char *symfile;
  188.  
  189. /* Look up a coff type-number index.  Return the address of the slot
  190.    where the type for that index is stored.
  191.    The type-number is in INDEX. 
  192.  
  193.    This can be used for finding the type associated with that index
  194.    or for associating a new type with the index.  */
  195.  
  196. static struct type **
  197. coff_lookup_type (index)
  198.      register int index;
  199. {
  200.   if (index >= type_vector_length)
  201.     {
  202.       int old_vector_length = type_vector_length;
  203.  
  204.       type_vector_length *= 2;
  205.       if (type_vector_length < index) {
  206.     type_vector_length = index * 2;
  207.       }
  208.       type_vector = (struct typevector *)
  209.     xrealloc (type_vector, sizeof (struct typevector)
  210.                 + type_vector_length * sizeof (struct type *));
  211.       bzero (&type_vector->type[ old_vector_length ],
  212.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  213.     }
  214.   return &type_vector->type[index];
  215. }
  216.  
  217. /* Make sure there is a type allocated for type number index
  218.    and return the type object.
  219.    This can create an empty (zeroed) type object.  */
  220.  
  221. static struct type *
  222. coff_alloc_type (index)
  223.      int index;
  224. {
  225.   register struct type **type_addr = coff_lookup_type (index);
  226.   register struct type *type = *type_addr;
  227.  
  228.   /* If we are referring to a type not known at all yet,
  229.      allocate an empty type for it.
  230.      We will fill it in later if we find out how.  */
  231.   if (type == 0)
  232.     {
  233.       type = (struct type *) obstack_alloc (symbol_obstack,
  234.                         sizeof (struct type));
  235.       bzero (type, sizeof (struct type));
  236.       *type_addr = type;
  237.     }
  238.   return type;
  239. }
  240.  
  241. /* maintain the lists of symbols and blocks */
  242.  
  243. /* Add a symbol to one of the lists of symbols.  */
  244. static void
  245. add_symbol_to_list (symbol, listhead)
  246.      struct symbol *symbol;
  247.      struct pending **listhead;
  248. {
  249.   register struct pending *link
  250.     = (struct pending *) xmalloc (sizeof (struct pending));
  251.  
  252.   link->next = *listhead;
  253.   link->symbol = symbol;
  254.   *listhead = link;
  255. }
  256.  
  257. /* Take one of the lists of symbols and make a block from it.
  258.    Put the block on the list of pending blocks.  */
  259.  
  260. static void
  261. finish_block (symbol, listhead, old_blocks, start, end)
  262.      struct symbol *symbol;
  263.      struct pending **listhead;
  264.      struct pending_block *old_blocks;
  265.      CORE_ADDR start, end;
  266. {
  267.   register struct pending *next, *next1;
  268.   register struct block *block;
  269.   register struct pending_block *pblock;
  270.   struct pending_block *opblock;
  271.   register int i;
  272.  
  273.   /* Count the length of the list of symbols.  */
  274.  
  275.   for (next = *listhead, i = 0; next; next = next->next, i++);
  276.  
  277.   block = (struct block *)
  278.         obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  279.  
  280.   /* Copy the symbols into the block.  */
  281.  
  282.   BLOCK_NSYMS (block) = i;
  283.   for (next = *listhead; next; next = next->next)
  284.     BLOCK_SYM (block, --i) = next->symbol;
  285.  
  286.   BLOCK_START (block) = start;
  287.   BLOCK_END (block) = end;
  288.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  289.  
  290.   /* Put the block in as the value of the symbol that names it.  */
  291.  
  292.   if (symbol)
  293.     {
  294.       SYMBOL_BLOCK_VALUE (symbol) = block;
  295.       BLOCK_FUNCTION (block) = symbol;
  296.     }
  297.   else
  298.     BLOCK_FUNCTION (block) = 0;
  299.  
  300.   /* Now free the links of the list, and empty the list.  */
  301.  
  302.   for (next = *listhead; next; next = next1)
  303.     {
  304.       next1 = next->next;
  305.       free (next);
  306.     }
  307.   *listhead = 0;
  308.  
  309.   /* Install this block as the superblock
  310.      of all blocks made since the start of this scope
  311.      that don't have superblocks yet.  */
  312.  
  313.   opblock = 0;
  314.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  315.     {
  316.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  317.     BLOCK_SUPERBLOCK (pblock->block) = block;
  318.       opblock = pblock;
  319.     }
  320.  
  321.   /* Record this block on the list of all blocks in the file.
  322.      Put it after opblock, or at the beginning if opblock is 0.
  323.      This puts the block in the list after all its subblocks.  */
  324.  
  325.   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  326.   pblock->block = block;
  327.   if (opblock)
  328.     {
  329.       pblock->next = opblock->next;
  330.       opblock->next = pblock;
  331.     }
  332.   else
  333.     {
  334.       pblock->next = pending_blocks;
  335.       pending_blocks = pblock;
  336.     }
  337. }
  338.  
  339. static struct blockvector *
  340. make_blockvector ()
  341. {
  342.   register struct pending_block *next, *next1;
  343.   register struct blockvector *blockvector;
  344.   register int i;
  345.  
  346.   /* Count the length of the list of blocks.  */
  347.  
  348.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  349.  
  350.   blockvector = (struct blockvector *)
  351.           obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  352.  
  353.   /* Copy the blocks into the blockvector.
  354.      This is done in reverse order, which happens to put
  355.      the blocks into the proper order (ascending starting address).
  356.      finish_block has hair to insert each block into the list
  357.      after its subblocks in order to make sure this is true.  */
  358.  
  359.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  360.   for (next = pending_blocks; next; next = next->next)
  361.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  362.  
  363.   /* Now free the links of the list, and empty the list.  */
  364.  
  365.   for (next = pending_blocks; next; next = next1)
  366.     {
  367.       next1 = next->next;
  368.       free (next);
  369.     }
  370.   pending_blocks = 0;
  371.  
  372.   return blockvector;
  373. }
  374.  
  375. /* Manage the vector of line numbers.  */
  376.  
  377. static
  378. record_line (line, pc)
  379.      int line;
  380.      CORE_ADDR pc;
  381. {
  382. }
  383.  
  384. /* Start a new symtab for a new source file.
  385.    This is called when a COFF ".file" symbol is seen;
  386.    it indicates the start of data for one original source file.  */
  387.  
  388. static void
  389. start_symtab ()
  390. {
  391.   context_stack = 0;
  392.   within_function = 0;
  393.   last_source_file = 0;
  394.  
  395.   /* Initialize the source file information for this file.  */
  396.  
  397. }
  398.  
  399. /* Finish the symbol definitions for one main source file,
  400.    close off all the lexical contexts for that file
  401.    (creating struct block's for them), then make the
  402.    struct symtab for that file and put it in the list of all such.
  403.  
  404.    END_ADDR is the address of the end of the file's text.  */
  405.  
  406. static void
  407. end_symtab (start_addr, end_addr, linetable)
  408.      CORE_ADDR start_addr, end_addr;
  409.      struct linetable *linetable;
  410. {
  411.   register struct symtab *symtab;
  412.   register struct context_stack *cstk;
  413.   register struct blockvector *blockvector;
  414.   struct partial_symtab *psymtab;
  415.  
  416.   if (aout_hdr.entry < end_addr
  417.       && aout_hdr.entry >= start_addr)
  418.     {
  419.       startup_file_start = start_addr;
  420.       startup_file_end = end_addr;
  421.     }
  422.  
  423.   /* Finish the lexical context of the last function in the file.  */
  424.  
  425.   if (context_stack)
  426.     {
  427.       cstk = context_stack;
  428.       context_stack = 0;
  429.       /* Make a block for the local symbols within.  */
  430.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  431.             cstk->start_addr, end_addr);
  432.       free (cstk);
  433.     }
  434.  
  435.   /* Create the two top-level blocks for this file.  */
  436.   finish_block (0, &file_symbols, 0, start_addr, end_addr);
  437.   finish_block (0, &global_symbols, 0, start_addr, end_addr);
  438.  
  439.   /* Create the blockvector that points to all the file's blocks.  */
  440.   blockvector = make_blockvector ();
  441.  
  442.   /* Now create the symtab object for this source file.  */
  443.   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
  444.   symtab->free_ptr = 0;
  445.  
  446.   /* Fill in its components.  */
  447.   symtab->blockvector = blockvector;
  448.   symtab->free_code = free_linetable;
  449.   symtab->filename = last_source_file;
  450.   symtab->linetable = linetable;
  451.   symtab->nlines = 0;
  452.   symtab->line_charpos = 0;
  453.  
  454.   /* Link the new symtab into the list of such.  */
  455.   symtab->next = symtab_list;
  456.   symtab_list = symtab;
  457.  
  458.   /* Reinitialize for beginning of new file. */
  459.   last_source_file = 0;
  460.  
  461.   /* Create a fake partial_symtab, so that find_pc_partial_function
  462.    * will do the right thing. */
  463.   psymtab = (struct partial_symtab *)
  464.       obstack_alloc (psymbol_obstack,
  465.              sizeof (struct partial_symtab));
  466.   bzero (psymtab, sizeof (struct partial_symtab));
  467.   psymtab->next = partial_symtab_list;
  468.   partial_symtab_list = psymtab;
  469.   psymtab->textlow = start_addr;
  470.   psymtab->texthigh = end_addr;
  471.   psymtab->filename = symtab->filename ? symtab->filename : "";
  472.   psymtab->readin = 1;
  473.  
  474. }
  475.  
  476. /* Accumulate the misc functions in bunches of 127.
  477.    At the end, copy them all into one newly allocated structure.  */
  478.  
  479. #define MISC_BUNCH_SIZE 127
  480.  
  481. struct misc_bunch
  482. {
  483.   struct misc_bunch *next;
  484.   struct misc_function contents[MISC_BUNCH_SIZE];
  485. };
  486.  
  487. /* Bunch currently being filled up.
  488.    The next field points to chain of filled bunches.  */
  489.  
  490. static struct misc_bunch *misc_bunch;
  491.  
  492. /* Number of slots filled in current bunch.  */
  493.  
  494. static int misc_bunch_index;
  495.  
  496. /* Total number of misc functions recorded so far.  */
  497.  
  498. static int misc_count;
  499.  
  500. static void
  501. init_misc_functions ()
  502. {
  503.   misc_count = 0;
  504.   misc_bunch = 0;
  505.   misc_bunch_index = MISC_BUNCH_SIZE;
  506. }
  507.  
  508. static void
  509. record_misc_function (name, address)
  510.      char *name;
  511.      CORE_ADDR address;
  512. {
  513.   register struct misc_bunch *new;
  514.  
  515.   if (misc_bunch_index == MISC_BUNCH_SIZE)
  516.     {
  517.       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
  518.       misc_bunch_index = 0;
  519.       new->next = misc_bunch;
  520.       misc_bunch = new;
  521.     }
  522.   misc_bunch->contents[misc_bunch_index].name = savestring (name, strlen (name));
  523.   misc_bunch->contents[misc_bunch_index].address = address;
  524.   misc_bunch->contents[misc_bunch_index].type = (char)mf_unknown;
  525.   misc_bunch_index++;
  526.   misc_count++;
  527. }
  528.  
  529. /* if we see a function symbol, we do record_misc_function.
  530.  * however, if it turns out the next symbol is '.bf', then
  531.  * we call here to undo the misc definition
  532.  */
  533. static void
  534. unrecord_misc_function ()
  535. {
  536.   if (misc_bunch_index == 0)
  537.     error ("Internal error processing symbol table, at symbol %d.",
  538.        symnum);
  539.   misc_bunch_index--;
  540.   misc_count--;
  541. }
  542.  
  543.  
  544. static int
  545. compare_misc_functions (fn1, fn2)
  546.      struct misc_function *fn1, *fn2;
  547. {
  548.   /* Return a signed result based on unsigned comparisons
  549.      so that we sort into unsigned numeric order.  */
  550.   if (fn1->address < fn2->address)
  551.     return -1;
  552.   if (fn1->address > fn2->address)
  553.     return 1;
  554.   return 0;
  555. }
  556.  
  557. static void
  558. discard_misc_bunches ()
  559. {
  560.   register struct misc_bunch *next;
  561.  
  562.   while (misc_bunch)
  563.     {
  564.       next = misc_bunch->next;
  565.       free (misc_bunch);
  566.       misc_bunch = next;
  567.     }
  568. }
  569.  
  570. static void
  571. condense_misc_bunches ()
  572. {
  573.   register int i, j;
  574.   register struct misc_bunch *bunch;
  575. #ifdef NAMES_HAVE_UNDERSCORE
  576.   int offset = 1;
  577. #else
  578.   int offset = 0;
  579. #endif
  580.  
  581.   misc_function_vector
  582.     = (struct misc_function *)
  583.       xmalloc (misc_count * sizeof (struct misc_function));
  584.  
  585.   j = 0;
  586.   bunch = misc_bunch;
  587.   while (bunch)
  588.     {
  589.       for (i = 0; i < misc_bunch_index; i++)
  590.     {
  591.       register char *tmp;
  592.  
  593.       misc_function_vector[j] = bunch->contents[i];
  594.       tmp = misc_function_vector[j].name;
  595.       misc_function_vector[j].name = (tmp[0] == '_' ? tmp + offset : tmp);
  596.       j++;
  597.     }
  598.       bunch = bunch->next;
  599.       misc_bunch_index = MISC_BUNCH_SIZE;
  600.     }
  601.  
  602.   misc_function_count = j;
  603.  
  604.   /* Sort the misc functions by address.  */
  605.  
  606.   qsort (misc_function_vector, j, sizeof (struct misc_function),
  607.      compare_misc_functions);
  608. }
  609.  
  610. /* Call sort_syms to sort alphabetically
  611.    the symbols of each block of each symtab.  */
  612.  
  613. static int
  614. compare_symbols (s1, s2)
  615.      struct symbol **s1, **s2;
  616. {
  617.   /* Names that are less should come first.  */
  618.   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  619.   if (namediff != 0) return namediff;
  620.   /* For symbols of the same name, registers should come first.  */
  621.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  622.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  623. }
  624.  
  625. static void
  626. sort_syms ()
  627. {
  628.   register struct symtab *s;
  629.   register int i, nbl;
  630.   register struct blockvector *bv;
  631.   register struct block *b;
  632.  
  633.   for (s = symtab_list; s; s = s->next)
  634.     {
  635.       bv = BLOCKVECTOR (s);
  636.       nbl = BLOCKVECTOR_NBLOCKS (bv);
  637.       for (i = 0; i < nbl; i++)
  638.     {
  639.       b = BLOCKVECTOR_BLOCK (bv, i);
  640.       if (BLOCK_SHOULD_SORT (b))
  641.           qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  642.              sizeof (struct symbol *), compare_symbols);
  643.     }
  644.     }
  645. }
  646.  
  647. /* Call sort_proc_descs to sort the procedure descriptors by address.  */
  648.  
  649. static int
  650. compare_proc_descs (p1, p2)
  651. PDR *p1, *p2;
  652. {
  653.   unsigned int a1, a2;
  654.  
  655.   a1 = PROC_LOW_ADDR (p1);
  656.   a2 = PROC_LOW_ADDR (p2);
  657.   if (a1 < a2)
  658.     return -1;
  659.   if (a1 > a2)
  660.     return 1;
  661.   return 0;
  662. }
  663.  
  664. static void
  665. sort_proc_descs ()
  666. {
  667.   qsort (proc_desc_table, proc_desc_length,
  668.      sizeof (PDR), compare_proc_descs);
  669. }
  670.  
  671.  
  672. /* This is the symbol-file command.  Read the file, analyze its symbols,
  673.    and add a struct symtab to symtab_list.  */
  674.  
  675. /* !!! !!! */
  676. int dump_stuff=0;
  677.  
  678. static void free_proc_descs ()
  679. {
  680.     if (proc_desc_table != NULL) {
  681.     free (proc_desc_table);
  682.     proc_desc_table = NULL;
  683.     proc_desc_length = 0;
  684.     }
  685. }
  686.  
  687. void
  688. symbol_file_command (name)
  689.      char *name;
  690. {
  691.   int desc;
  692.   int num_symbols;
  693.   int num_sections;
  694.   int symtab_offset;
  695.   register int val;
  696.   struct cleanup *old_chain;
  697.  
  698.   dont_repeat ();
  699.  
  700.   if (name == 0)
  701.     {
  702.       if (symtab_list && !query ("Discard symbol table? ", 0))
  703.     error ("Not confirmed.");
  704.       if (symfile)
  705.     free (symfile);
  706.       symfile = 0;
  707.       free_all_symtabs ();
  708.       free_proc_descs ();
  709.       return;
  710.     }
  711.  
  712.   name = tilde_expand (name);
  713.   make_cleanup (free, name);
  714.  
  715.   if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
  716.     error ("Not confirmed.");
  717.  
  718.   if (symfile)
  719.     free (symfile);
  720.   symfile = 0;
  721.  
  722.   {
  723.     char *absolute_name;
  724.  
  725.     desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  726.     if (desc < 0)
  727.       perror_with_name (name);
  728.     else
  729.       name = absolute_name;
  730.   }
  731.  
  732.   old_chain = make_cleanup (close, desc);
  733.   make_cleanup (free_current_contents, &name);
  734.  
  735.   if ((num_symbols = read_file_hdr (desc, &file_hdr)) < 0)
  736.     error ("File \"%s\" not in executable format.", name);
  737.  
  738.   /* If an a.out header is present, read it in.  If not (e.g. a .o file)
  739.      deal with its absence.  */
  740.   if (file_hdr.f_opthdr == 0
  741.       || read_aout_hdr (desc, &aout_hdr, file_hdr.f_opthdr) < 0)
  742.     {
  743.       /* We will not actually be able to run code, since backtraces would
  744.      fly off the bottom of the stack (there is no way to reliably
  745.      detect bottom of stack), but that's fine since the kernel won't
  746.      run something without an a.out header anyway.  Passive examination
  747.      of .o files is one place this might make sense.  */
  748.       /* ~0 will not be in any file.  */
  749.       aout_hdr.entry = ~0;
  750.       /* set the startup file to be an empty range.  */
  751.       startup_file_start = 0;
  752.       startup_file_end = 0;
  753.     }
  754.  
  755.   if (num_symbols == 0)
  756.     {
  757.       free_all_symtabs ();
  758.       free_proc_descs ();
  759.       printf ("%s does not have a symbol-table.\n", name);
  760.       fflush (stdout);
  761.       do_cleanups (old_chain);
  762.       return;
  763.     }
  764.  
  765.   printf ("Reading symbol data from %s...", name);
  766.   fflush (stdout);
  767.  
  768.   /* Throw away the old symbol table.  */
  769.  
  770.   free_all_symtabs ();
  771.   free_proc_descs ();
  772.   free_all_psymtabs ();        /* Make sure that partial_symtab_list */
  773.                 /* is 0 also. */
  774.  
  775.   num_sections = file_hdr.f_nscns;
  776.   symtab_offset = file_hdr.f_symptr;
  777.  
  778.   if (read_section_hdr (desc, _TEXT, &text_hdr, num_sections) < 0)
  779.     error ("\"%s\": can't read text section header", name);
  780.  
  781.   /* Position to read the symbol table.  Do not read it all at once. */
  782.   val = lseek (desc, (long)symtab_offset, 0);
  783.   if (val < 0)
  784.     perror_with_name (name);
  785.  
  786.   init_misc_functions ();
  787.   make_cleanup (discard_misc_bunches, 0);
  788.  
  789.   /* Now that the executable file is positioned at symbol table,
  790.      process it and define symbols accordingly.  */
  791.  
  792.   val = read_coff_symtab (desc, num_symbols, symtab_offset);
  793.   if (val < 0) error("Bad symbol table format");
  794.  
  795.   patch_opaque_types ();
  796.  
  797.   /* Sort symbols alphabetically within each block.  */
  798.  
  799.   sort_syms ();
  800.  
  801.   /* Go over the misc functions and install them in vector.  */
  802.  
  803.   condense_misc_bunches ();
  804.  
  805.   /* Sort the procedure descriptor table.  */
  806.  
  807.   sort_proc_descs ();
  808.  
  809.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  810.  
  811.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  812.  
  813.   /* Make a default for file to list.  */
  814.  
  815.   symfile = savestring (name, strlen (name));
  816.  
  817.   do_cleanups (old_chain);
  818.  
  819.   printf ("done.\n");
  820.   fflush (stdout);
  821. }
  822.  
  823. /* Return name of file symbols were loaded from, or 0 if none..  */
  824.  
  825. char *
  826. get_sym_file ()
  827. {
  828.   return symfile;
  829. }
  830.  
  831. /* Simplified internal version of coff symbol table information */
  832.  
  833. struct coff_symbol {
  834.   char *c_name;
  835.   int c_symnum;        /* symbol number of this entry */
  836.   int c_nsyms;        /* 1 if syment only, 2 if syment + auxent */
  837.   long c_value;
  838.   int c_sclass;
  839.   int c_secnum;
  840.   unsigned int c_type;
  841. };
  842.  
  843. static char *
  844. read_table(desc, size, offset)
  845.     long offset;
  846. {
  847.     char *buf;
  848.     if (lseek(desc, offset, 0) < 0)
  849.       error("Bad symbol table format [seek]");
  850.     buf = xmalloc(size);
  851.     if (buf == NULL) error("Not enough memory");
  852.     if (myread (desc, buf, size) != size)
  853.       error("Bad symbol table format [read]");
  854.     return buf;
  855. }
  856.  
  857. static char * (MapStNames[]) = { /* symbol type names */
  858.     "Nil", "Global", "Static", "Param", "Local", "Label", "Proc", "Block",
  859.     "End", "Member", "Typedef", "File", "RegReloc", "Forward", "StaticProc",
  860.     "Constant", "BlockPatched"
  861. };
  862.  
  863. long stNameSize = sizeof(MapStNames)/sizeof(MapStNames[0]);
  864.  
  865. static char * MapScNames[] = {    /* storage class names */
  866.     "Nil", "Text", "Data", "Bss", "Register", "Abs", "Undefined", "CdbLocal",
  867.     "Bits", "Dbx", "RegImage", "Info", "UserStruct", "SData", "SBss", "RData",
  868.     "Var", "Common", "SCommon", "VarRegister", "Variant", "SUndefined", "Init"
  869. };
  870.  
  871.  
  872. long scNameSize = sizeof(MapScNames)/sizeof(MapScNames[0]);
  873.  
  874. static char *(MapBtNames[]) = {    /* base type names */
  875.     "Nil", "Adr", "Char", "UChar", "Short", "UShort", "Int", "UInt", "Long",
  876.     "ULong", "Float", "Double", "Struct", "Union", "Enum", "Typedef", "Range",
  877.     "Set", "Complex", "DComplex", "Indirect", "FixedDec", "FloatDec", "String",
  878.     "Bit", "Picture"
  879. };
  880.  
  881. long btNameSize = sizeof(MapBtNames)/sizeof(MapBtNames[0]);
  882.  
  883. /* Given pointers to a symbol table in coff style exec file,
  884.    analyze them and create struct symtab's describing the symbols.
  885.    NSYMS is the number of symbols in the symbol table.
  886.    We read them one at a time using read_one_sym ().  */
  887.  
  888. /* stBlockPatched indicates an stBlock symbol which has been patched
  889.  * so that the value points to s struct type */
  890.  
  891. #define stBlockPatched 16
  892.  
  893. static FDR *file_descriptor_table;
  894. static RFDT *rel_file_table;
  895. static SYMR *local_symbol_table;
  896. static char *local_string_table;
  897. static AUXU *aux_symbol_table;
  898. static FDR *cur_file_descriptor;
  899. static int file_descriptor_count;
  900. /* Note that PDR is also used for frame chaining, and is defined above */
  901.  
  902. static int cur_proc_number; /* number of procedure within current file */
  903. static CORE_ADDR cur_proc_addr;
  904. static int cur_file_number;
  905. static int cur_isymBase;
  906. static int cur_issBase;
  907.  
  908. static void
  909. select_file(i)
  910.     int i;
  911. {
  912.     global_symbols_all[cur_file_number] = global_symbols;
  913.     file_symbols_all[cur_file_number] = file_symbols;
  914.     global_symbols = global_symbols_all[i];
  915.     file_symbols = file_symbols_all[i];
  916.     cur_file_number = i;
  917.     cur_file_descriptor = &file_descriptor_table[i];
  918.     cur_isymBase = cur_file_descriptor->isymBase;
  919.     cur_issBase = cur_file_descriptor->issBase;
  920. }
  921.  
  922. /* side-effect: changes cur_file_descriptor */
  923.  
  924. static SYMR *
  925. get_type_context(auxp)
  926.     AUXU** auxp;
  927. {
  928.   SYMR *sym;
  929.   FDR *sym_file_desc;
  930.   int rfi;
  931.   int rfd = (*auxp)->rndx.rfd;
  932.   int sym_index = (*auxp)->rndx.index;
  933.   (*auxp)++;
  934.   if (rfd == ST_RFDESCAPE) { rfd = (*auxp)->isym; (*auxp)++; }
  935.   if (rfd == ST_EXTIFD || sym_index == ST_ANONINDEX) return NULL;
  936.   rfi = rel_file_table[cur_file_descriptor->rfdBase + rfd];
  937.   if (rfi >= file_descriptor_count) return NULL;
  938.   sym_file_desc = &file_descriptor_table[rfi];
  939.   if (sym_index >= sym_file_desc->csym || sym_index == 0) return NULL;
  940.   sym = &local_symbol_table[sym_file_desc->isymBase + sym_index];
  941.   if (sym->index == 0) return NULL;
  942.   select_file(rfi);
  943.   if (dump_stuff)
  944.     printf("rfd(%d,%d)->[%s,%x,st%s,sc%s,inx:%d (%x)]\n",
  945.      rfi, sym_index,
  946.      &local_string_table[cur_issBase+sym->iss],
  947.      sym->value,
  948.            MapStNames[sym->st], MapScNames[sym->sc],
  949.            sym->index, sym);
  950.   return sym;
  951. }
  952.  
  953. static struct type *
  954. get_struct_type(sym, kind)
  955.      SYMR *sym;
  956.      int kind; /* either btStruct or btUnion or btNil (unknown) */
  957. {
  958.   struct type *type;
  959.   if (sym->st == stBlockPatched)
  960.     {
  961.       type = (struct type*)sym->value;
  962.       /* Patch up possibly-wrong guess about struct vs. union */
  963.       if (kind == btStruct) TYPE_CODE(type) = TYPE_CODE_STRUCT;
  964.       else if (kind == btUnion) TYPE_CODE(type) = TYPE_CODE_UNION;
  965.     }
  966.   else if (sym->st != stBlock)
  967.     {
  968.       fprintf(stderr,"Bad symbol table: struct/union points to non-stBlock\n");
  969.       type = builtin_type_void;
  970.     }
  971.   else
  972.     {
  973.       int nfields = 0;
  974.       SYMR *first_field = sym+1;
  975.       register struct field *cur_field;
  976.       char *sym_name = &local_string_table[cur_issBase+sym->iss];
  977.       int iauxBase = cur_file_descriptor->iauxBase;
  978.       type = (struct type *) obstack_alloc (symbol_obstack,
  979.                         sizeof (struct type));
  980.       bzero (type, sizeof (struct type));
  981.       TYPE_LENGTH (type) = sym->value;
  982.  
  983.       /* we "remember" the type generated from this block */
  984.       sym->st = stBlockPatched;
  985.       sym->value = (long)type;
  986.  
  987.       /* first count the number of fields */
  988.       for (sym = first_field; sym->st != stEnd; sym++)
  989.     if (sym->st == stMember) nfields++;
  990.     else if (sym->st == stBlock || sym->st == stBlockPatched)
  991.       {
  992.         if (sym->sc == scVariant) ; /* UNIMPLEMENTED */
  993.         if (sym->index != 0)
  994.           sym = &local_symbol_table[cur_isymBase + sym->index - 1];
  995.       }
  996.  
  997.       TYPE_NFIELDS (type) = nfields;
  998.       TYPE_FIELDS (type) = cur_field = (struct field*)
  999.     obstack_alloc (symbol_obstack, nfields * sizeof (struct field));
  1000.       for (sym = first_field; sym->st != stEnd; sym++)
  1001.     if (sym->st == stMember)
  1002.       {
  1003.         AUXU *aux = &aux_symbol_table[iauxBase + sym->index];
  1004.         char *name = &local_string_table[cur_issBase+sym->iss];
  1005.         cur_field->name =
  1006.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1007.  
  1008.         cur_field->type = read_type(sym->index, &cur_field->bitsize);
  1009.         cur_field->bitpos = sym->value;
  1010.         cur_field++;
  1011.       }
  1012.     else if (sym->st == stTypedef) ; /* just ignore it */
  1013.     else if (sym->st == stBlock || sym->st == stBlockPatched)
  1014.       {
  1015.         if (sym->sc == scVariant) ; /* UNIMPLEMENTED */
  1016.         if (sym->index != 0)
  1017.           sym = &local_symbol_table[cur_isymBase + sym->index - 1];
  1018.       }
  1019.     else
  1020.       fprintf(stderr, "[Bad member in struct/union field list]\n");
  1021.       /*
  1022.        * Heuristic: if 2nd field has offset 0, this is a union,
  1023.        * otherwise, a struct definition.
  1024.        * If nfields <= 1, we guess struct.
  1025.        * If the type is used later, we fix it up.
  1026.        * (This is done in the stBlockPatched case at the top of this routine).
  1027.        */
  1028.       if (kind == btNil && nfields > 1)
  1029.     {
  1030.       if (TYPE_FIELDS(type)[1].bitpos > 0) kind = btStruct;
  1031.       else kind = btUnion;
  1032.     }
  1033.       TYPE_CODE (type) = kind == btUnion ? TYPE_CODE_UNION : TYPE_CODE_STRUCT;
  1034.       TYPE_NAME (type) = concat ("",
  1035.                  (kind == btUnion ? "union " : "struct "),
  1036.                  sym_name);
  1037.     }
  1038.     return type;
  1039. }
  1040.  
  1041. static struct type *
  1042. read_struct_type(auxp, kind)
  1043.      AUXU** auxp;
  1044.      int kind; /* either btStruct or btUnion */
  1045. {
  1046.   struct type *type;
  1047.   int save_file_number = cur_file_number;
  1048.   SYMR *sym = get_type_context(auxp);
  1049.   if (sym == NULL) {
  1050.     /*
  1051.      * If we get here then there is reference to a structure that is never
  1052.      * defined.  Such a thing is perfectly legal (e.g. a pointer to a
  1053.      * structure that is never defined), and the error message is annoying.
  1054.      */
  1055. #ifndef sprite
  1056.     printf(stderr, "Bad symbol for struct/union definition\n");
  1057. #endif
  1058.     return builtin_type_void;
  1059.   }
  1060.   if (sym->st == stTypedef)
  1061.       type = read_type(sym->index, NULL);
  1062.   else
  1063.       type = get_struct_type(sym, kind);
  1064.   select_file (save_file_number);
  1065.   return type;
  1066. }
  1067.  
  1068. static struct type *
  1069. get_enum_type(sym)
  1070.     SYMR *sym;
  1071. {
  1072.   struct type *type;
  1073.   if (sym->st == stBlockPatched)
  1074.     type = (struct type*)sym->value;
  1075.   else if (sym->st != stBlock)
  1076.     {
  1077.       fprintf(stderr,"Bad symbol table: enum points to non-stBlock\n");
  1078.       type = builtin_type_void;
  1079.     }
  1080.   else
  1081.     {
  1082.       int nfields = 0;
  1083.       SYMR *first_field = sym+1;
  1084.       register struct field *cur_field;
  1085.       type = (struct type *) obstack_alloc (symbol_obstack,
  1086.                         sizeof (struct type));
  1087.       bzero (type, sizeof (struct type));
  1088.       TYPE_LENGTH (type) = sym->value;
  1089.  
  1090.       /* we "remember" the type generated from this block */
  1091.       sym->st = stBlockPatched;
  1092.       sym->value = (long)type;
  1093.  
  1094.       TYPE_CODE (type) = TYPE_CODE_ENUM;
  1095.       TYPE_LENGTH (type) = sizeof (int);
  1096.       TYPE_NAME (type) = concat ("", "enum ",
  1097.                  &local_string_table[cur_issBase+sym->iss]);
  1098.  
  1099.       nfields = &local_symbol_table[cur_isymBase+sym->index] - first_field - 1;
  1100.  
  1101.       TYPE_NFIELDS (type) = nfields;
  1102.       TYPE_FIELDS (type) = cur_field = (struct field*)
  1103.     obstack_alloc (symbol_obstack, nfields * sizeof (struct field));
  1104.       for (sym = first_field; sym->st != stEnd; sym++)
  1105.     if (sym->st == stMember)
  1106.       {
  1107.         char *name = &local_string_table[cur_issBase+sym->iss];
  1108.         cur_field->name =
  1109.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1110.  
  1111.         cur_field->bitpos = sym->value;
  1112.         cur_field->bitsize = 0;
  1113.         cur_field++;
  1114.       }
  1115.     else
  1116.       fprintf(stderr, "[Bad member in enum list]\n");
  1117.     }
  1118.   return type;
  1119. }
  1120.  
  1121. static struct type *
  1122. read_enum_type(auxp)
  1123.      AUXU** auxp;
  1124. {
  1125.   struct type *type;
  1126.   int save_file_number = cur_file_number;
  1127.   SYMR *sym = get_type_context(auxp);
  1128.   if (sym == NULL) {
  1129.     fprintf(stderr, "Bad symbol for enum definition");
  1130.     return builtin_type_int;
  1131.   }
  1132.   if (sym->st == stTypedef)
  1133.       type = read_type(sym->index, NULL);
  1134.   else
  1135.       type = get_enum_type (sym);
  1136.   select_file (save_file_number);
  1137.   return type;
  1138. }
  1139.  
  1140. static struct type *
  1141. read_range_type(auxp)
  1142.      AUXU** auxp;
  1143. {
  1144.   struct type *range_type;
  1145.   int save_file_number = cur_file_number;
  1146.   SYMR *sym = get_type_context(auxp);
  1147.   /* sym (if non-NULL) may point at an stBlock/scInfo, but ignore it */
  1148.    range_type = (struct type *) obstack_alloc (symbol_obstack,
  1149.                           sizeof (struct type));
  1150.   TYPE_CODE (range_type) = TYPE_CODE_RANGE;
  1151.   TYPE_TARGET_TYPE (range_type) = builtin_type_int;
  1152.   TYPE_LENGTH (range_type) = sizeof (int);
  1153.   TYPE_NFIELDS (range_type) = 2;
  1154.   TYPE_FIELDS (range_type) =
  1155.       (struct field *) obstack_alloc (symbol_obstack,
  1156.                       2 * sizeof (struct field));
  1157.   TYPE_FIELD_BITPOS (range_type, 0) = (*auxp)++->dnLow;
  1158.   TYPE_FIELD_BITPOS (range_type, 1) = (*auxp)++->dnHigh;
  1159.  
  1160.   select_file (save_file_number);
  1161.   return range_type;
  1162. }
  1163.  
  1164. static struct type *
  1165. modify_type(type, qualifier, auxp)
  1166.      struct type *type;
  1167.      int qualifier;
  1168.      AUXU** auxp;
  1169. {
  1170.     switch (qualifier) {
  1171.       case tqPtr: return lookup_pointer_type(type);
  1172.       case tqNil: return type;
  1173.       case tqArray:
  1174.     {
  1175.         struct type *range_type = read_range_type(auxp);
  1176.         long lower = TYPE_FIELD_BITPOS (range_type, 0);
  1177.         long upper = TYPE_FIELD_BITPOS (range_type, 1);
  1178.         int elem_size = (*auxp)++->width; /* not used */
  1179.         struct type *atype;
  1180.  
  1181.         atype = (struct type *)
  1182.         obstack_alloc (symbol_obstack, sizeof (struct type));
  1183.         bzero (atype, sizeof (struct type));
  1184.  
  1185.         TYPE_CODE (atype) = TYPE_CODE_ARRAY;
  1186.         TYPE_TARGET_TYPE (atype) = type;
  1187.         TYPE_LENGTH (atype) = (upper - lower + 1) * TYPE_LENGTH (type);
  1188.         TYPE_NFIELDS (atype) = 1;
  1189.         TYPE_FIELDS (atype) =
  1190.         (struct field *) obstack_alloc (symbol_obstack,
  1191.                         sizeof (struct field));
  1192.         TYPE_FIELD_TYPE (atype, 0) = range_type;
  1193.         return atype;
  1194.     }
  1195.       case tqVol: return type;
  1196.       case tqProc: return lookup_function_type(type);
  1197.       default:
  1198.     fprintf(stderr, "[Unimplemented type qualifier: %d]\n", qualifier);
  1199.     return type;
  1200.     }
  1201. }
  1202.  
  1203. static struct type *
  1204. apply_type_modifiers(type, tip, auxp)
  1205.      struct type *type;
  1206.      TIR *tip;
  1207.      AUXU** auxp;
  1208. {
  1209.     for (;;) {   
  1210.     if (tip->tq0 == tqNil) return type;
  1211.     type = modify_type(type, tip->tq0, auxp);
  1212.     if (tip->tq1 == tqNil) return type;
  1213.     type = modify_type(type, tip->tq1, auxp);
  1214.     if (tip->tq2 == tqNil) return type;
  1215.     type = modify_type(type, tip->tq2, auxp);
  1216.     if (tip->tq3 == tqNil) return type;
  1217.     type = modify_type(type, tip->tq3, auxp);
  1218.     if (tip->tq4 == tqNil) return type;
  1219.     type = modify_type(type, tip->tq4, auxp);
  1220.     if (tip->tq5 == tqNil) return type;
  1221.     type = modify_type(type, tip->tq5, auxp);
  1222.     if (!tip->continued) return type;
  1223.     tip++;
  1224.     }
  1225. }
  1226.  
  1227. static struct type *
  1228. read_type(index, widthp)
  1229.      int index;
  1230.      int *widthp; /* if non-NULL: set to (if bit field: width, else: 0) */
  1231. {
  1232.     TIR *tip;
  1233.     AUXU *aux;
  1234.     struct type *base_type;
  1235.     int width;
  1236.     if (index == 0xfffff) return builtin_type_int; /* no type info */
  1237. /*    if (index < 0 || index > ) ...; */
  1238.  
  1239.     aux = &aux_symbol_table[cur_file_descriptor->iauxBase + index];
  1240.     tip = &aux->ti;
  1241.     for ( ; aux->ti.continued; aux++) ;
  1242.     aux++; /* skip last TIR field */
  1243.  
  1244.     /* sym.h claims that width comes after RNDX, but seems to be wrong */
  1245.     width = tip->fBitfield ? (aux++)->width : 0;
  1246.     if (widthp) *widthp = width;
  1247.  
  1248.     switch (tip->bt) {
  1249.       case btNil: base_type = builtin_type_void; break;
  1250.       case btAdr: base_type = lookup_pointer_type(builtin_type_void); break;
  1251.       case btChar: base_type = builtin_type_char; break;
  1252.       case btUChar: base_type = builtin_type_unsigned_char; break;
  1253.       case btShort: base_type = builtin_type_short; break;
  1254.       case btUShort: base_type = builtin_type_unsigned_short; break;
  1255.       case btInt: base_type = builtin_type_int; break;
  1256.       case btUInt: base_type = builtin_type_unsigned_int; break;
  1257.       case btLong: base_type = builtin_type_long; break;
  1258.       case btULong: base_type = builtin_type_unsigned_long; break;
  1259.       case btFloat: base_type = builtin_type_float; break;
  1260.       case btDouble: base_type = builtin_type_double; break;
  1261.       case btStruct: case btUnion:
  1262.     base_type = read_struct_type(&aux, tip->bt); break;
  1263.       case btEnum:
  1264.     base_type = read_enum_type(&aux); break;
  1265.       case btRange:
  1266.     base_type = read_range_type(&aux); break;
  1267.       case btTypedef:
  1268.       case btSet: case btComplex: case btDComplex:
  1269.       case btIndirect:
  1270.       case btFixedDec: case btFloatDec: case btString:
  1271.       case btBit: case btPicture:
  1272.       default:
  1273.         /*
  1274.      * Don't complain about void types (26). 
  1275.      */
  1276.         if (tip->bt != 26) {
  1277.         fprintf(stderr, "[Unimplemented kind of type: %d]\n", tip->bt);
  1278.     }
  1279.     base_type = builtin_type_void;
  1280.     }
  1281.     return apply_type_modifiers(base_type, tip, &aux);   
  1282. }
  1283.  
  1284. static struct symbol *
  1285. alloc_symbol(name, value)
  1286.   char *name;
  1287.   int value;
  1288. {
  1289.   register struct symbol *sym;
  1290.   sym = (struct symbol *)obstack_alloc (symbol_obstack, sizeof(struct symbol));
  1291. #ifdef NAMES_HAVE_UNDERSCORE
  1292.   if (name[0] == '_') name++;
  1293. #endif
  1294.  
  1295.   bzero (sym, sizeof (struct symbol));
  1296.   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
  1297.  
  1298.   /* default assumptions */
  1299.   SYMBOL_VALUE (sym) = value;
  1300.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1301.   SYMBOL_TYPE (sym) = builtin_type_void;
  1302.  
  1303.   return sym;
  1304. }
  1305.  
  1306. static
  1307. read_symbol(csym, name)
  1308.   SYMR *csym;
  1309.   char *name;
  1310. {
  1311.   register struct symbol *sym = alloc_symbol(name, csym->value);
  1312.  
  1313.   switch (csym->st)
  1314.     {
  1315.     case stNil: break;
  1316.     case stFile:
  1317.       last_source_file = obstack_copy0 (symbol_obstack, name, strlen (name));
  1318.       break;
  1319.     case stGlobal:
  1320.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1321.       SYMBOL_CLASS (sym) = LOC_STATIC;
  1322.       add_symbol_to_list (sym, &global_symbols);
  1323.       break;
  1324.     case stStatic:
  1325.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1326.       SYMBOL_CLASS (sym) = LOC_STATIC;
  1327.       if (within_function) {
  1328.       /* Static symbol of local scope */
  1329.       add_symbol_to_list (sym, &local_symbols);
  1330.       }
  1331.       else {
  1332.       /* Static symbol at top level of file */
  1333.       add_symbol_to_list (sym, &file_symbols);
  1334.       }
  1335.       break;
  1336.  
  1337.     case stLocal:
  1338.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1339.       switch (csym->sc) {
  1340.     case scRegister: SYMBOL_CLASS (sym) = LOC_REGISTER; break;
  1341.     case scAbs:    SYMBOL_CLASS (sym) = LOC_LOCAL; break;
  1342.  
  1343.       }
  1344.       add_symbol_to_list (sym, &local_symbols);
  1345.       break;
  1346.  
  1347.     case stParam:
  1348.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1349.       switch (csym->sc) {
  1350.     case scAbs:      SYMBOL_CLASS (sym) = LOC_ARG; break;
  1351.     case scRegister:  SYMBOL_CLASS (sym) = LOC_REGPARM; break;
  1352.     case scVar:      SYMBOL_CLASS (sym) = LOC_REF_ARG; break;
  1353.     case scVarRegister: SYMBOL_CLASS (sym) = LOC_REGPARM; break; /*WRONG!*/
  1354.       }
  1355.       add_symbol_to_list (sym, &local_symbols);
  1356.       break;
  1357.  
  1358.     case stTypedef:
  1359.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1360.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1361.       if (within_function) {
  1362.       /* Static symbol of local scope */
  1363.       add_symbol_to_list (sym, &local_symbols);
  1364.       }
  1365.       else {
  1366.       /* Static symbol at top level of file */
  1367.       add_symbol_to_list (sym, &file_symbols);
  1368.       }
  1369.       break;
  1370.     
  1371.     case stEnd:
  1372.     default: break;
  1373.     }
  1374.   return 1;
  1375. }
  1376.  
  1377. static int
  1378. read_tagged_block (index)
  1379. {
  1380.   SYMR *csym = &local_symbol_table[cur_isymBase+index];
  1381.   char *name = &local_string_table[cur_issBase+csym->iss];
  1382.   struct pending **symlist = within_function ? &local_symbols: &file_symbols;
  1383.   register struct symbol *sym;
  1384.  
  1385.   /* A kludge to decide if this is an enum definition. */
  1386.   if ((csym[1].st == stMember
  1387.        && read_type(csym[1].index, 0) == builtin_type_void)
  1388.    || (csym->st == stBlockPatched
  1389.        && TYPE_CODE((struct type*)(csym->value)) == TYPE_CODE_ENUM))
  1390.     {
  1391.       struct type *type = get_enum_type (csym);
  1392.       if (type && TYPE_CODE(type) == TYPE_CODE_ENUM)
  1393.         {
  1394.           int nfields = TYPE_NFIELDS (type);
  1395.           register int i;
  1396.           register struct field *cur_field = TYPE_FIELDS(type);
  1397.           if (name && name[0] && name[0] != '.')
  1398.         {
  1399.           sym = alloc_symbol(name, 0);
  1400.           SYMBOL_TYPE (sym) = type;
  1401.           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1402.           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1403.           add_symbol_to_list (sym, symlist);
  1404.         }
  1405.           for (i = nfields; --i >= 0; cur_field++)
  1406.         {
  1407.           sym = alloc_symbol(cur_field->name, cur_field->bitpos);
  1408.           SYMBOL_CLASS (sym) = LOC_CONST;
  1409.           SYMBOL_TYPE (sym) = type;
  1410.           add_symbol_to_list (sym, symlist);
  1411.         }
  1412.           return nfields + 2;
  1413.       }
  1414.       }
  1415.   else
  1416.     {
  1417.       if (name && name[0] && name[0] != '.')
  1418.     {
  1419.       struct type *type = get_struct_type (csym, btNil);
  1420.       sym = alloc_symbol(name, 0);
  1421.       SYMBOL_TYPE (sym) = type;
  1422.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1423.       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1424.       add_symbol_to_list (sym, symlist);
  1425.     }
  1426.       return read_symbols (index + 1) + 2; 
  1427.     }
  1428. }
  1429.  
  1430. /* Read symbols until an stEnd symbol is encountered. Return count. */
  1431.  
  1432. static int
  1433. read_symbols(index)
  1434.      int index; /* relative to current file */
  1435. {
  1436.   register struct context_stack *new;
  1437.   static int blocks_seen = 0;
  1438.   int index0 = index;
  1439.   SYMR *start_symbol;
  1440.   for (;;)
  1441.       {
  1442.     SYMR *csym = &local_symbol_table[cur_isymBase+index];
  1443.     char *name = &local_string_table[cur_issBase+csym->iss];
  1444.  
  1445.     if (dump_stuff)
  1446.         printf("[%s,%x,st%s,sc%s,inx:%d (%x)]\n", name, csym->value,
  1447.            MapStNames[csym->st], MapScNames[csym->sc],
  1448.            csym->index, csym);
  1449.  
  1450.     switch (csym->st)
  1451.         {
  1452.         case stFile:
  1453.           last_source_file =
  1454.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1455.           index++;
  1456.           index += read_symbols(index) + 1;
  1457.           return index - index0;
  1458.         case stBlock:
  1459.         case stBlockPatched:
  1460.           if (csym->sc == scText)
  1461.         { /* lexical block */
  1462.           if (!local_symbol_table[cur_isymBase+csym->index-1].value)
  1463.             {
  1464.               new = 0;
  1465.             }
  1466.           else
  1467.             {
  1468.               new = (struct context_stack *)
  1469.             xmalloc (sizeof (struct context_stack));
  1470.               new->next = context_stack;
  1471.               context_stack = new;
  1472.               new->locals = local_symbols;
  1473.               new->old_blocks = pending_blocks;
  1474.               new->start_addr = cur_proc_addr + csym->value;
  1475.               new->name = 0;
  1476.               local_symbols = 0;
  1477.             }
  1478.           blocks_seen++;
  1479.           start_symbol=csym;
  1480.           index++;
  1481.           index += read_symbols (index);
  1482.           csym = &local_symbol_table[cur_isymBase+index];  /* stEnd */
  1483.           index++;
  1484.  
  1485. if (csym->st != stEnd ||
  1486.    start_symbol != &local_symbol_table[cur_isymBase+csym->index])
  1487. abort();
  1488.           if (new)
  1489.             {
  1490.               if (local_symbols && context_stack->next)
  1491.             {
  1492.               /* Make a block for the local symbols within.  */
  1493.               finish_block (0, &local_symbols, new->old_blocks,
  1494.                     new->start_addr,
  1495.                     cur_proc_addr + csym->value);
  1496.             }
  1497.               local_symbols = new->locals;
  1498.               context_stack = new->next;
  1499.               free (new);
  1500.             }
  1501.           }
  1502.           else if (csym->sc == scInfo)
  1503.           index += read_tagged_block (index);
  1504.           break;
  1505.         case stProc:
  1506.         case stStaticProc:
  1507.           {
  1508.         PDR *proc = &proc_desc_table
  1509.             [cur_file_descriptor->ipdFirst+cur_proc_number++];
  1510.         CORE_ADDR saved_cur_proc_addr = cur_proc_addr;
  1511.         struct symbol *sym = alloc_symbol(name, csym->value);
  1512.         struct context_stack *saved_context_stack = context_stack;
  1513.         int save_blocks_seen = blocks_seen;
  1514.         SYMBOL_CLASS (sym) = LOC_BLOCK;
  1515.         /* Add one to csym->index, to the skip the isymMac value
  1516.          * (which is there only for st*Proc). */
  1517.         SYMBOL_TYPE (sym) = 
  1518.             lookup_function_type (read_type(csym->index+1, NULL));
  1519.         if (csym->st == stStaticProc)
  1520.             add_symbol_to_list (sym, &file_symbols);
  1521.         else
  1522.             add_symbol_to_list (sym, &global_symbols);
  1523.         cur_proc_addr = csym->value;
  1524.         within_function++;
  1525.         new = (struct context_stack *)
  1526.             xmalloc (sizeof (struct context_stack));
  1527.         new->next = 0;
  1528.         context_stack = new;
  1529.         new->locals = 0;
  1530.         new->old_blocks = pending_blocks;
  1531.         new->start_addr = csym->value;
  1532.         new->name = sym;
  1533.         if (dump_stuff)
  1534.             printf("[PROC:%s,@@%x,framereg:%d,froff:%d,rmsk:%d,fmsk:%d,floff:%d,regoff:%d,iline:%d,lnLo:%d,lnHi:%d (%x)]\n",
  1535.                name, proc->adr,
  1536.            proc->framereg, proc->frameoffset, proc->regmask,
  1537.            proc->fregmask, proc->fregoffset,
  1538.                proc->regoffset,
  1539.                proc->iline,proc->lnLow,proc->lnHigh, proc);
  1540.  
  1541.         start_symbol = csym;
  1542.         index++;
  1543.         index += read_symbols(index);
  1544.  
  1545.         csym = &local_symbol_table[cur_isymBase+index];/*stEnd symbol*/
  1546.  
  1547. if (csym->st != stEnd ||
  1548.    start_symbol != &local_symbol_table[cur_isymBase+csym->index])
  1549. abort();
  1550.  
  1551.         index++;
  1552.         PROC_SYMBOL(proc) =
  1553.             blocks_seen == save_blocks_seen ? NULL : sym;
  1554.         PROC_LOW_ADDR(proc) = start_symbol->value;
  1555.         PROC_HIGH_ADDR(proc) = last_end_addr =
  1556.             start_symbol->value + csym->value;
  1557.  
  1558.         finish_block (new->name, &local_symbols, new->old_blocks,
  1559.                   start_symbol->value, last_end_addr);
  1560.         context_stack = saved_context_stack;
  1561.         cur_proc_addr = saved_cur_proc_addr;
  1562.         within_function--;
  1563.         free (new);
  1564.         break;
  1565.           }
  1566.  
  1567.         case stEnd:
  1568. #if 1
  1569.             return index - index0;
  1570. #else
  1571.           start_symbol = &local_symbol_table[cur_isymBase+csym->index];
  1572.           switch (start_symbol->st)
  1573.           {
  1574.           case stProc:
  1575.           case stStaticProc:
  1576.           case stFile:
  1577.           case stBlock:
  1578.           case stBlockPatched:
  1579.             return index - index0;
  1580.           default:
  1581.             index++;
  1582.           }
  1583.           break;
  1584. #endif
  1585.         default:
  1586.           read_symbol(csym, name);
  1587.           index++;
  1588.         }
  1589.       }
  1590. }
  1591.  
  1592. static int
  1593. read_coff_symtab (desc, nsyms, symtab_offset)
  1594.      int desc;
  1595.      int nsyms;
  1596.      int symtab_offset;
  1597. {
  1598.   HDRR hdrr;
  1599.   struct coff_symbol coff_symbol;
  1600.   register struct coff_symbol *cs = &coff_symbol;
  1601.   struct coff_symbol fcn_cs_saved;
  1602.  
  1603.   int num_object_files = 0;
  1604.   int next_file_symnum = -1;
  1605.   char *filestring;
  1606.   int fcn_first_line;
  1607.   int fcn_last_line;
  1608.   int fcn_start_addr;
  1609.   long fcn_line_ptr;
  1610.   struct cleanup *file_chain, *ext_chain, *old_chain;
  1611.   int isym, ifile;
  1612.  
  1613.   char *line_number_table;
  1614.   EXTR *external_symbol_table;
  1615.   char *external_string_table;
  1616.   struct linetable **line_vector_all;
  1617.  
  1618.   if (myread (desc, (char *)&hdrr, sizeof hdrr) != sizeof hdrr)
  1619.     return -1;
  1620.  
  1621.   file_descriptor_count = hdrr.ifdMax;
  1622.   line_vector_all = (struct linetable**)
  1623.       alloca (file_descriptor_count * sizeof(struct linetable *));
  1624.   global_symbols_all = (struct pending**)
  1625.       alloca (file_descriptor_count * sizeof(struct pending *));
  1626.   file_symbols_all = (struct pending**)
  1627.       alloca (file_descriptor_count * sizeof(struct pending *));
  1628.   proc_desc_length = hdrr.ipdMax;
  1629.   proc_desc_table = (PDR*)
  1630.     read_table(desc, hdrr.ipdMax * sizeof(PDR), hdrr.cbPdOffset);
  1631.   old_chain = make_cleanup (free_all_symtabs, 0);
  1632.   make_cleanup (free_proc_descs, 0);
  1633.   file_descriptor_table = (FDR*)
  1634.     read_table(desc, hdrr.ifdMax * sizeof(FDR), hdrr.cbFdOffset);
  1635.   file_chain = make_cleanup (free, file_descriptor_table);
  1636.  
  1637.   line_number_table = read_table(desc, hdrr.cbLine, hdrr.cbLineOffset);
  1638.   ext_chain = make_cleanup (free, line_number_table);
  1639.  
  1640.   for (ifile = 0; ifile < hdrr.ifdMax; ifile++)
  1641.     {
  1642.       int iproc, ipdMax;
  1643.       char *cur_line_entry;
  1644.       int cur_addr_offset = 0;
  1645.       int cur_addr;
  1646.  
  1647.       /* Vector of line number information.  */
  1648.       struct linetable *line_vector;
  1649.  
  1650.       /* Index of next entry to go in line_vector_index.  */
  1651.       int line_vector_index;
  1652.  
  1653.       /* Number of elements allocated for line_vector currently.  */
  1654.       int line_vector_length;
  1655.  
  1656.       global_symbols_all[ifile] = 0;
  1657.       file_symbols_all[ifile] = 0;
  1658.       select_file (ifile);
  1659.  
  1660.       /* read line numbers */
  1661.       cur_addr = cur_file_descriptor->adr;
  1662.       iproc = cur_file_descriptor->ipdFirst;
  1663.  
  1664.       line_vector_index = 0;
  1665.       line_vector_length = 1000;
  1666.       line_vector = (struct linetable *)
  1667.       xmalloc (sizeof (struct linetable)
  1668.            + line_vector_length * sizeof (struct linetable_entry));
  1669.  
  1670.       /* we read the line numbers first, since read_symbol over-writes
  1671.      some of the fields in a proc descriptor */
  1672.       ipdMax = iproc + cur_file_descriptor->cpd;
  1673.       cur_line_entry = line_number_table + cur_file_descriptor->cbLineOffset;
  1674.       for ( ; iproc < ipdMax; iproc++) {
  1675.     PDR *proc = &proc_desc_table[iproc];
  1676.     int nlines = (iproc+1 < ipdMax ? (proc+1)->iline : cur_file_descriptor->cline)
  1677.         - proc->iline;
  1678.     int cur_source_line = proc->lnLow;
  1679.     if (dump_stuff)
  1680.         printf(
  1681.         "[proc:%x,framereg:%d,ofset:%d,rmask:%d,flmask:%d,floff:%d,iline:%d,lnLo:%d,lnHi:%d,csl:%d]\n",
  1682.            proc->adr,
  1683.            proc->framereg, proc->frameoffset, proc->regmask,
  1684.            proc->fregmask, proc->fregoffset,
  1685.            proc->iline,proc->lnLow,proc->lnHigh, cur_source_line);
  1686.     if (proc->iline == -1) continue;
  1687.     for (isym = proc->iline; nlines > 0; isym++) {
  1688.       struct linetable_entry *e;
  1689.       int code = *cur_line_entry++;
  1690.       int delta = code >> 4;
  1691.       int count = (code & 15) + 1;
  1692.       if (dump_stuff & 1) printf("%2x", 0xFF & code);
  1693.       if (delta == -8)
  1694.         {
  1695.           if (dump_stuff & 1)
  1696.             printf(" %2x%2x", 0xff & cur_line_entry[0],
  1697.              0xff & cur_line_entry[1]);
  1698.           delta = *cur_line_entry++;
  1699.           delta = (delta << 8) | (unsigned)*cur_line_entry++;
  1700.           isym += 2;
  1701.         }
  1702.       cur_source_line += delta;
  1703.       if (dump_stuff & 1)
  1704.         printf("\t%2d.%2d li:%d %X", delta, count,
  1705.          cur_source_line,
  1706.          cur_file_descriptor->adr + 4 * cur_addr_offset);
  1707.  
  1708.       e = &line_vector->item[line_vector_index];
  1709.       if (line_vector_index == 0 || e[-1].line != cur_source_line)
  1710.         {
  1711.  
  1712.           /* Make sure line vector is big enough.  */
  1713.           if (line_vector_index + 2 >= line_vector_length)
  1714.         {
  1715.           line_vector_length *= 2;
  1716.           line_vector = (struct linetable *)
  1717.             xrealloc (line_vector, sizeof (struct linetable)
  1718.                   + (line_vector_length
  1719.                  * sizeof (struct linetable_entry)));
  1720.           e = &line_vector->item[line_vector_index];
  1721.         }
  1722.  
  1723.           e->line = cur_source_line;
  1724.           e->pc = cur_file_descriptor->adr + 4 * cur_addr_offset;
  1725.           line_vector_index++;
  1726.         }
  1727.  
  1728.       if (delta != 0)
  1729.         {
  1730.           int save_addr = cur_addr;
  1731.           if (cur_addr_offset != 0)
  1732.         {
  1733.           int toffset = (cur_addr_offset - 1) * 4;
  1734.           if (dump_stuff & 1)
  1735.           printf(" [li: %d %x-%x]",
  1736.              cur_source_line, cur_addr, cur_addr+toffset);
  1737.         }
  1738.           cur_addr = save_addr + cur_addr_offset * 4;
  1739.         }
  1740.       cur_addr_offset += count;
  1741.       nlines -= count;
  1742.       if (dump_stuff & 1) putchar('\n');
  1743.     /*  if (delta != 0) { } */
  1744.         }
  1745.       }
  1746.  
  1747.       line_vector->nitems = line_vector_index;
  1748.       line_vector_all[ifile] = (struct linetable *)
  1749.       xrealloc (line_vector, (sizeof (struct linetable)
  1750.                + line_vector_index * sizeof (struct linetable_entry)));
  1751.     }
  1752.   do_cleanups (ext_chain);
  1753.  
  1754.   aux_symbol_table = (AUXU*)
  1755.     read_table(desc, hdrr.iauxMax * sizeof(AUXU), hdrr.cbAuxOffset);
  1756.   make_cleanup (free, aux_symbol_table);
  1757.   rel_file_table = (RFDT*)
  1758.     read_table(desc, hdrr.crfd * sizeof(RFDT), hdrr.cbRfdOffset);
  1759.   make_cleanup (free, rel_file_table);
  1760.   local_symbol_table = (SYMR*)
  1761.     read_table(desc, hdrr.isymMax * sizeof(SYMR), hdrr.cbSymOffset);
  1762.   make_cleanup (free, local_symbol_table);
  1763.   local_string_table = read_table(desc, hdrr.issMax, hdrr.cbSsOffset);
  1764.   make_cleanup (free, local_string_table);
  1765.  
  1766.   external_symbol_table = (EXTR*)
  1767.     read_table(desc, hdrr.iextMax * sizeof(EXTR), hdrr.cbExtOffset);
  1768.   ext_chain = make_cleanup (free, external_symbol_table);
  1769.   external_string_table = read_table(desc, hdrr.issExtMax, hdrr.cbSsExtOffset);
  1770.   make_cleanup (free, external_string_table);
  1771.  
  1772.   /* do the external symbols first */
  1773.   /* The reason is that each file's end_symtab is done with the locals */
  1774.  
  1775.   for (isym = 0; isym < hdrr.iextMax; isym++)
  1776.     {
  1777.       EXTR *ext = &external_symbol_table[isym];
  1778.       SYMR *csym = &ext->asym;
  1779.       char *name = external_string_table+csym->iss;
  1780.       if ((unsigned)ext->ifd >= hdrr.ifdMax) return -1;
  1781.       select_file (ext->ifd);
  1782.       record_misc_function (name, csym->value);
  1783.       if (csym->st != stProc && csym->st != stStaticProc)
  1784.       read_symbol(csym, name);
  1785. #if 1
  1786. if (dump_stuff)
  1787.       printf("EXT[%s,%x,st%s,sc%s,inx:%d,ifd:%d]\n",
  1788.          name, csym->value, MapStNames[csym->st],
  1789.          MapScNames[csym->sc], csym->index, ext->ifd);
  1790. #endif
  1791.     }
  1792.  
  1793.   do_cleanups (ext_chain);
  1794.  
  1795.   for (ifile = 0; ifile < hdrr.ifdMax; ifile++)
  1796.     {
  1797.       select_file (ifile);
  1798.  
  1799.       cur_proc_number = 0;
  1800.       last_end_addr = cur_file_descriptor->adr;
  1801.  
  1802.       start_symtab ();
  1803.  
  1804. if (dump_stuff)
  1805. printf("<FILE.adr:%x,ilB:%d,cb:%d,cbLO:%d,cbL:%d,iSB:%d,cS:%d,nProcs:%d,first:%d>\n",
  1806.        cur_file_descriptor->adr,
  1807.        cur_file_descriptor->ilineBase, cur_file_descriptor->cline,
  1808.        cur_file_descriptor->cbLineOffset, cur_file_descriptor->cbLine,
  1809.        cur_isymBase, cur_file_descriptor->csym,
  1810.        cur_file_descriptor->cpd,
  1811.        cur_file_descriptor->ipdFirst);
  1812.  
  1813.       for (isym = 0; isym < cur_file_descriptor->csym; )
  1814.       {
  1815.         int count = read_symbols(isym);
  1816.         if (count == 0)
  1817.         { /* handle un-handled stEnd */
  1818.           SYMR *sym = &local_symbol_table[cur_isymBase+isym];
  1819.           char *name = &local_string_table[cur_issBase+sym->iss];
  1820. printf("stEnd confusion!");
  1821.           read_symbol(sym, name);
  1822.           count = 1;
  1823.         }
  1824.         isym += count;
  1825.       }
  1826.       end_symtab (cur_file_descriptor->adr, last_end_addr,
  1827.           line_vector_all[ifile]);
  1828.     }
  1829.  
  1830.   do_cleanups (file_chain);
  1831.  
  1832.   last_source_file = 0;
  1833.   bzero (opaque_type_chain, sizeof opaque_type_chain);
  1834.  
  1835.   type_vector_length = 160;
  1836.   type_vector = (struct typevector *)
  1837.         xmalloc (sizeof (struct typevector)
  1838.                 + type_vector_length * sizeof (struct type *));
  1839.   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
  1840.  
  1841.   if (last_source_file)
  1842.     end_symtab ();
  1843.   discard_cleanups (old_chain);
  1844.   return 0;
  1845. }
  1846.  
  1847. /* Routines for reading headers and symbols from executable.  */
  1848.  
  1849. /* Read COFF file header, check magic number,
  1850.    and return number of symbols. */
  1851. read_file_hdr (chan, file_hdr)
  1852.     int chan;
  1853.     FILHDR *file_hdr;
  1854. {
  1855.   lseek (chan, 0L, 0);
  1856.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1857.     return -1;
  1858.  
  1859.   switch (file_hdr->f_magic)
  1860.     {
  1861. #ifdef MIPSEBMAGIC
  1862.     case MIPSEBMAGIC:
  1863. #endif
  1864. #ifdef MIPSELMAGIC
  1865.     case MIPSELMAGIC:
  1866. #endif
  1867. #ifdef MC68MAGIC
  1868.     case MC68MAGIC:
  1869. #endif
  1870. #ifdef NS32GMAGIC
  1871.       case NS32GMAGIC:
  1872.       case NS32SMAGIC:
  1873. #endif
  1874. #ifdef I386MAGIC
  1875.     case I386MAGIC:
  1876. #endif
  1877. #ifdef CLIPPERMAGIC
  1878.     case CLIPPERMAGIC:
  1879. #endif      
  1880.     return file_hdr->f_nsyms;
  1881.  
  1882.       default:
  1883. #ifdef BADMAG
  1884.     if (BADMAG(file_hdr))
  1885.       return -1;
  1886.     else
  1887.       return file_hdr->f_nsyms;
  1888. #else
  1889.     return -1;
  1890. #endif
  1891.     }
  1892. }
  1893.  
  1894. read_aout_hdr (chan, aout_hdr, size)
  1895.     int chan;
  1896.     AOUTHDR *aout_hdr;
  1897.     int size;
  1898. {
  1899.   lseek (chan, (long)FILHSZ, 0);
  1900.   if (size != sizeof (AOUTHDR))
  1901.     return -1;
  1902.   if (myread (chan, (char *)aout_hdr, size) != size)
  1903.     return -1;
  1904.   return 0;
  1905. }
  1906.  
  1907. read_section_hdr (chan, section_name, section_hdr, nsects)
  1908.     register int chan;
  1909.     register char *section_name;
  1910.     SCNHDR *section_hdr;
  1911.     register int nsects;
  1912. {
  1913.   register int i;
  1914.  
  1915.   if (lseek (chan, FILHSZ + sizeof (AOUTHDR), 0) < 0)
  1916.     return -1;
  1917.  
  1918.   for (i = 0; i < nsects; i++)
  1919.     {
  1920.       if (myread (chan, (char *)section_hdr, SCNHSZ) < 0)
  1921.     return -1;
  1922.       if (strncmp (section_hdr->s_name, section_name, 8) == 0)
  1923.     return 0;
  1924.     }
  1925.     return -1;
  1926. }
  1927.  
  1928.  
  1929. /* Support for string table handling */
  1930.  
  1931. static char *stringtab = NULL;
  1932.  
  1933.  
  1934. #if 0
  1935. static char *
  1936. getsymname (symbol_entry)
  1937.     SYMENT *symbol_entry;
  1938. {
  1939.   static char buffer[SYMNMLEN+1];
  1940.   char *result;
  1941.  
  1942.   if (symbol_entry->n_zeroes == 0)
  1943.     {
  1944.       result = stringtab + symbol_entry->n_offset;
  1945.     }
  1946.   else
  1947.     {
  1948.       strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
  1949.       buffer[SYMNMLEN] = '\0';
  1950.       result = buffer;
  1951.     }
  1952.   return result;
  1953. }
  1954. #endif
  1955.  
  1956. /* Support for line number handling */
  1957. static char *linetab = NULL;
  1958. static long linetab_offset;
  1959. static int linetab_count;
  1960.  
  1961. static int
  1962. hashname (name)
  1963.      char *name;
  1964. {
  1965.   register char *p = name;
  1966.   register int total = p[0];
  1967.   register int c;
  1968.  
  1969.   c = p[1];
  1970.   total += c << 2;
  1971.   if (c)
  1972.     {
  1973.       c = p[2];
  1974.       total += c << 4;
  1975.       if (c)
  1976.     total += p[3] << 6;
  1977.     }
  1978.   
  1979.   return total % HASHSIZE;
  1980. }
  1981.  
  1982. static void
  1983. patch_type (type, real_type)
  1984.     struct type *type;
  1985.     struct type *real_type;
  1986. {
  1987.   register struct type *target = TYPE_TARGET_TYPE (type);
  1988.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1989.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1990.  
  1991.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1992.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1993.   TYPE_FIELDS (target) = (struct field *)
  1994.                 obstack_alloc (symbol_obstack, field_size);
  1995.  
  1996.   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
  1997.  
  1998.   if (TYPE_NAME (real_target))
  1999.     {
  2000.       if (TYPE_NAME (target))
  2001.     free (TYPE_NAME (target));
  2002.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
  2003.     }
  2004. }
  2005.  
  2006. /* Patch up all appropriate typdef symbols in the opaque_type_chains
  2007.    so that they can be used to print out opaque data structures properly */
  2008.  
  2009. static void
  2010. patch_opaque_types ()
  2011. {
  2012.   struct symtab *s;
  2013.  
  2014.   /* Look at each symbol in the per-file block of each symtab.  */
  2015.   for (s = symtab_list; s; s = s->next)
  2016.     {
  2017.       register struct block *b;
  2018.       register int i;
  2019.  
  2020.       /* Go through the per-file symbols only */
  2021.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
  2022.       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  2023.     {
  2024.       register struct symbol *real_sym;
  2025.  
  2026.       /* Find completed typedefs to use to fix opaque ones.
  2027.          Remove syms from the chain when their types are stored,
  2028.          but search the whole chain, as there may be several syms
  2029.          from different files with the same name.  */
  2030.       real_sym = BLOCK_SYM (b, i);
  2031.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  2032.           SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  2033.           TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  2034.           TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  2035.         {
  2036.           register char *name = SYMBOL_NAME (real_sym);
  2037.           register int hash = hashname (name);
  2038.           register struct symbol *sym, *prev;
  2039.  
  2040.           prev = 0;
  2041.           for (sym = opaque_type_chain[hash]; sym;)
  2042.         {
  2043.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  2044.               !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
  2045.             {
  2046.               if (prev)
  2047.             SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
  2048.               else
  2049.             opaque_type_chain[hash]
  2050.               = (struct symbol *) SYMBOL_VALUE (sym);
  2051.  
  2052.               patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  2053.  
  2054.               if (prev)
  2055.             sym = (struct symbol *) SYMBOL_VALUE (prev);
  2056.               else
  2057.             sym = opaque_type_chain[hash];
  2058.             }
  2059.           else
  2060.             {
  2061.               prev = sym;
  2062.               sym = (struct symbol *) SYMBOL_VALUE (sym);
  2063.             }
  2064.         }
  2065.         }
  2066.     }
  2067.     }
  2068. }
  2069.  
  2070. /* This function is really horrible, but to avoid it, there would need
  2071.    to be more filling in of forward references.  THIS SHOULD BE MOVED
  2072.    OUT OF COFFREAD.C AND DBXREAD.C TO SOME PLACE WHERE IT CAN BE SHARED. */
  2073. int
  2074. fill_in_vptr_fieldno (type)
  2075.      struct type *type;
  2076. {
  2077.   if (TYPE_VPTR_FIELDNO (type) < 0)
  2078.     TYPE_VPTR_FIELDNO (type) =
  2079.       fill_in_vptr_fieldno (TYPE_BASECLASS (type, 1));
  2080.   return TYPE_VPTR_FIELDNO (type);
  2081. }
  2082.  
  2083. /* partial symbol tables are not implemented in coff, therefore
  2084.    block_for_pc() (and others) will never decide to call this. */
  2085.  
  2086. extern struct symtab *
  2087. psymtab_to_symtab ()
  2088. {
  2089.   fatal ("error: Someone called psymtab_to_symtab\n");
  2090. }
  2091.  
  2092. /* These will stay zero all the time */
  2093. struct psymbol_allocation_list global_psymbols, static_psymbols;
  2094. @
  2095.  
  2096.  
  2097. 1.3
  2098. log
  2099. @got rid of error generated by structure that is never defined.
  2100. @
  2101. text
  2102. @d1235 6
  2103. a1240 1
  2104.     fprintf(stderr, "[Unimplemented kind of type: %d]\n", tip->bt);
  2105. @
  2106.  
  2107.  
  2108. 1.2
  2109. log
  2110. @mips gdb port to Sprite
  2111. @
  2112. text
  2113. @d1012 8
  2114. a1019 1
  2115.     printf(stderr, "Bad symbol for struct/union definition");
  2116. d1091 1
  2117. a1091 1
  2118.     printf(stderr, "Bad symbol for enum definition");
  2119. @
  2120.  
  2121.  
  2122. 1.1
  2123. log
  2124. @Initial revision
  2125. @
  2126. text
  2127. @a1203 1
  2128.       case btVoid:
  2129. a1605 1
  2130.       select_file (ifile);
  2131. d1608 1
  2132. @
  2133.